package aaaBrowser.test;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Positions;

import org.eclipse.swt.browser.ProgressEvent;
import org.eclipse.swt.browser.ProgressListener;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

import aaBaolilo.ImageDemo;
import aaBaolilo.LoginAndPub;
import aaBaolilo.OcrX;
import aaaBrowser.CaptX;
import aaaBrowser.atiBrowserExample;

import com.attilax.Closure;
import com.attilax.core;
import com.attilax.io.filex;
import com.attilax.io.pathx;
import com.attilax.lang.ClipboardUtil;

/**
 * public static String USERNAME = "use2015"; //UU u public static String
 * PASSWORD = "000000";
 * 
 * @author Administrator
 *
 */
public class Login_finish_info {

	public Login_finish_info() {
		this.capt = new CaptX();
	}

	public static void main(String[] args) {

		new Login_finish_info().start();
	}

	final Display display = new Display();
	atiBrowserExample atiBrowserExampleinstance;

	private void start() {

		Shell shell = new Shell(display);
		shell.setLayout(new FillLayout());
		shell.setText("Browser example");
		// shell.setVisible(false);
		// shell.setSize(1, 1);
		atiBrowserExampleinstance = new atiBrowserExample(shell);

		shell.open(); // shell.setVisible(false);

		// core.execMeth_Ays(new Runnable() {
		//
		// @Override
		// public void run() {
		// atiBrowserExampleinstance.addProgressListener(pathx
		// .classPath(LoginAndPub.class) + "\\login.js");
		// atiBrowserExampleinstance
		// .gotoUrl("http://demo.destoon.com/v5.0/member/login.php?a0");
		//
		// }
		// }, "--");

		 login();
		//finish_info();

		// atiBrowserExampleinstance.ini();

		// atiBrowserExampleinstance.addProgressListener(pathx
		// .classPath(LoginAndPub.class) + "\\login.js");

		while (!shell.isDisposed()) {
			System.out.println("111");
			if (!display.readAndDispatch())
			{
				System.out.println("222");
				display.sleep();
			}
		}
		// icon.dispose();
		atiBrowserExampleinstance.dispose();
		display.dispose();
	}

	private void login() {
		core.execMeth_Ays(new Runnable() {

			@Override
			public void run() {
				System.out.println("=-ajsy");
				display.getDefault().asyncExec(new Runnable() {
					@Override
					public void run() {

						// atiBrowserExampleinstance
						// .gotoUrl("http://www.baidu.com");
						atiBrowserExampleinstance
								.gotoUrl("http://atidestoon.com/member/login.php");
						atiBrowserExampleinstance.addProgressListener(
								pathx.classPath(aaaBrowser.LoginAndPub.class)
										+ "\\login.js", new Closure() {

									@Override
									public Object execute(Object arg0)
											throws Exception {
										core.execMeth_Ays(new Runnable() {

											@Override
											public void run() {
												try {
													Thread.sleep(3000);
												} catch (InterruptedException e) {
													// TODO Auto-generated catch block
													e.printStackTrace();
												}
												finish_info();

											}
										}, "");

										return null;
									}

								});

						atiBrowserExampleinstance.regJsCallbackMeth(
								"getCaptch", new Closure() {

									@Override
									public Object execute(Object arg0)
											throws Exception {
										setCaptch();
										return null;
									}
								});

						atiBrowserExampleinstance.gotoUrl();
						// display.getDefault().asyncExec(new Runnable(){
						//
						// @Override
						// public void run() {
						// atiBrowserExampleinstance
						// .gotoUrl();
						//
						// }});

						// atiBrowserExampleinstance.waitFinish(display);

					}
				});

			}
		}, "");
	}

	private void finish_info() {
		System.out.println("---finish_info start");
		execMeth_AysGui(new Runnable() {

			@Override
			public void run() {
				atiBrowserExampleinstance.gotoUrl("http://atidestoon.com/member/edit.php?tab=2");
				atiBrowserExampleinstance.addProgressListener(
						pathx.classPath(Login_finish_info.class)
								+ "\\finish_info.js", new Closure() {

							@Override
							public Object execute(Object arg0) throws Exception {
								// pub();

								return null;
							}

						});
				atiBrowserExampleinstance.gotoUrl();
			}
		}, "--finis info");

	}

	private void reg() {
		core.execMeth_Ays(new Runnable() {

			@Override
			public void run() {
				System.out.println("=-ajsy");
				display.getDefault().asyncExec(new Runnable() {
					@Override
					public void run() {

						// atiBrowserExampleinstance
						// .gotoUrl("http://www.baidu.com");
						atiBrowserExampleinstance
								.gotoUrl("http://localhost/destoon/member/edit.php?tab=2");
						atiBrowserExampleinstance.addProgressListener(
								pathx.classPath(Login_finish_info.class)
										+ "\\reg.js", new Closure() {

									@Override
									public Object execute(Object arg0)
											throws Exception {
										// pub();

										return null;
									}

								});
						atiBrowserExampleinstance.regJsCallbackMeth(
								"getCaptch", new Closure() {

									@Override
									public Object execute(Object arg0)
											throws Exception {
										setCaptch();
										return null;
									}
								});

						atiBrowserExampleinstance.gotoUrl();

					}
				});

			}
		}, "");
	}

	CaptX capt;

	public void setCaptch() {

		String captchCode = capt.getTxt();

		String js = filex.read(pathx.classPath(Login_finish_info.class)
				+ "\\set_captch.js");
		js = js.replaceAll("@capt", captchCode);
		// "1234";
		System.out.println("exe js:" + js);
		atiBrowserExampleinstance.browser.execute(js);

	}

	private void execMeth_AysGui(final Runnable runnable, String string) {
		core.execMeth_Ays(new Runnable() {

			@SuppressWarnings("static-access")
			@Override
			public void run() {
				System.out.println("=-ajsy");
				// Display default1 = display.getDefault();
				// runnable.run();
				Display.getDefault().asyncExec(runnable);
				// display.asyncExec(runnable);
			}
		}, string);

	}

}
